DAY28:Sum of the first nth term of Series


Posted by birdbirdmurmur on 2023-08-10

題目連結

https://www.codewars.com/kata/555eded1ad94b00403000071

解法

function SeriesSum(n)
{
  let sum = 0
  for ( let i=0; i<n ; i++){
    sum += 1 / ( i * 3 + 1)
  }
//   return Math.round((sum + Number.EPSILON) * 100) / 100
  return sum.toFixed(2)
}

心得

不知道怎麼取小數點後兩位
爬了一下文
有兩種使用方式

第一種在數字後面加上toFixed(2)
這樣可以確保1可以變成1.00
而且可以直接變成字串

第二種是註解掉的那一條(但對1無效)
Math.round()用來四捨五入
Number.EPSILON是一個非常小的數字(為了解決浮點數的問題)
...* 100) / 100這是讓數字除完以後可以到小數點第二位










Related Posts

How to solve the perpetual loading issue in Evernote? Evernote 一直轉圈圈的解決辦法

How to solve the perpetual loading issue in Evernote? Evernote 一直轉圈圈的解決辦法

【隨堂筆記】Python 桌面應用程式

【隨堂筆記】Python 桌面應用程式

【Day02】 OM2M框架佈署與介紹

【Day02】 OM2M框架佈署與介紹


Comments